<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2017/8/1
  Time: 16:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
        body, html{width: 100%;height: 100%;margin:0;font-family:"微软雅黑";}
        #allmap {width: 100%; height:70%; overflow: hidden;}
        #result {width:100%;font-size:12px;}
        .BMap_cpyCtrl,.anchorBL{display: none!important;}/*隐藏百度地图版权*/
        .BMapLib_Drawing .BMapLib_marker {display: none!important;}/*绘制工具栏 隐藏 画点*/
        .BMapLib_Drawing .BMapLib_polyline {display: none!important;}/*绘制工具栏 隐藏 画折线*/
        /* .BMapLib_Drawing .BMapLib_circle {display: none!important;}*/ /*绘制工具栏 隐藏 画圆*/
        /* .BMapLib_Drawing .BMapLib_rectangle {display: none!important;}*/ /*绘制工具栏 隐藏 画矩形 */
        dl,dt,dd,ul,li{
            margin:0;
            padding:0;
            list-style:none;
        }
        p{font-size:12px;}
        dt{
            font-size:14px;
            font-family:"微软雅黑";
            font-weight:bold;
            border-bottom:1px dotted #000;
            padding:5px 0 5px 5px;
            margin:5px 0;
        }
        dd{
            padding:5px 0 0 5px;
        }
        li{
            line-height:28px;
        }
    </style>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=86354139cb6b3c6353d04891954ff18c"></script>
    <!--加载鼠标绘制工具-->
    <script type="text/javascript" src="http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.js"></script>
    <link rel="stylesheet" href="http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.css" />
    <!--加载检索信息窗口-->
    <script type="text/javascript" src="http://api.map.baidu.com/library/SearchInfoWindow/1.4/src/SearchInfoWindow_min.js"></script>
    <link rel="stylesheet" href="http://api.map.baidu.com/library/SearchInfoWindow/1.4/src/SearchInfoWindow_min.css" />
    <title>百度地图API功能演示</title>
</head>
<body>
<div id="allmap" style="overflow:hidden;zoom:1;position:relative;">
    <div id="map" style="height:100%;-webkit-transition: all 0.5s ease-in-out;transition: all 0.5s ease-in-out;">
    </div>
</div>
<div id="result">
    <%--<input type="button" value="获取绘制的覆盖物个数" onclick="alert(overlays.length)"/>--%>
    <input type="button" value="清除所有覆盖物" onclick="clearAll()"/>
    <input type="button"  value="生成坐标数据" onclick="getPoint()"/>
    <input type="button"  value="开启覆盖物编辑功能" onclick="Editing('enable')"/>
    <input type="button"  value="关闭覆盖物编辑功能" onclick="Editing('disable')"/> <br/>
        <input id="location" type="input" style="width: 600px" readonly="readonly"/>
        <input type="button" value="确定" onclick="save()"/><br/>
    <span>双击左键，结束编辑。画完区域后，请点击"生成坐标数据"，再点击确定按钮后即可保存数据</span>  <br/>
</div>
<%--<div id="resultShape"></div>--%>
<%--<div id="shape">坐标为</div>--%>

<script type="text/javascript">
    function $(id){
        return document.getElementById(id);
    }

    // 百度地图API功能
    var map = new BMap.Map('map');
    var poi = new BMap.Point(${coordinate});
    map.centerAndZoom(poi, 16);

    map.enableScrollWheelZoom();
    map.clearOverlays();        //清除地图覆盖物
    var marker = new BMap.Marker(poi);  // 创建标注
    map.addOverlay(marker);
    var styleOptions = {
        strokeColor:"red",    //边线颜色。
        fillColor:"red",      //填充颜色。当参数为空时，圆形将没有填充效果。
        strokeWeight: 3,       //边线的宽度，以像素为单位。
        strokeOpacity: 0.8,    //边线透明度，取值范围0 - 1。
        fillOpacity: 0.6,      //填充的透明度，取值范围0 - 1。
        strokeStyle: 'solid' //边线的样式，solid或dashed。
    };
    var a=${area};//[[108.324991,22.84072],[108.321326,22.836789],[108.324488,22.833192],[108.330058,22.836256],[108.328477,22.83649],[108.329052,22.837822],[108.329878,22.839154]];
//    alert(a.toString());
    //如果已有区域，则画出该区域，无，则定位用户位置
    if (a!==0){
        var b=eval(a)
        var p=new BMap.Polygon(
            getPoints(b),styleOptions);
        map.addOverlay(p);
        map.panTo(new BMap.Point(b[0][0],b[0][1]))
    }
    else{
//        var geolocation = new BMap.Geolocation();
//        geolocation.getCurrentPosition(function(r){
//            if(this.getStatus() == BMAP_STATUS_SUCCESS){
//                var mk = new BMap.Marker(r.point);
//                map.addOverlay(mk);
//                map.panTo(r.point);
//            }
//            else {
////            alert('failed'+this.getStatus());
//                alert("定位失败")
//            }
//        },{enableHighAccuracy: true});
    }


    function getPoints(a) {
        var cc=new Array();
        for (var i=0;i<a.length;i++){
            cc.push(new BMap.Point(a[i][0],a[i][1]))
        }
        return cc;
    }

    var overlays = [];
    var overlaycomplete = function(e){
        overlays.push(e.overlay);
    };


    //实例化鼠标绘制工具
    var drawingManager = new BMapLib.DrawingManager(map, {
        isOpen: false, //是否开启绘制模式
        enableDrawingTool: true, //是否显示工具栏
        drawingToolOptions: {
            anchor: BMAP_ANCHOR_TOP_RIGHT, //位置
            offset: new BMap.Size(5, 5), //偏离值
        },
        circleOptions: styleOptions, //圆的样式
        polylineOptions: styleOptions, //线的样式
        polygonOptions: styleOptions, //多边形的样式
        rectangleOptions: styleOptions //矩形的样式
    });

    //添加鼠标绘制工具监听事件，用于获取绘制结果
    drawingManager.addEventListener('overlaycomplete', overlaycomplete);

//    map.addEventListener("rightclick",function(e){
//        if(confirm(e.point.lng + "," + e.point.lat)){
//            $("shape").innerHTML=$("shape").innerHTML+" <br/>("+e.point.lng+","+e.point.lat+")";
//        }
//    });
    function draw(type){
        drawingManager.open();
        drawingManager.setDrawingMode(type);
    }

    function clearAll() {
        for(var i = 0; i < overlays.length; i++){
            map.removeOverlay(overlays[i]);
        }
        map.clearOverlays();
        overlays.length = 0
    }
    function getPoint(){
//        $("resultShape").innerHTML='';
//        for(var i = 0; i < overlays.length; i++){
//            var overlay=overlays[i].getPath();
//            $("resultShape").innerHTML=$("resultShape").innerHTML+overlay.length+'边形:<br/>';
//            for(var j = 0; j < overlay.length; j++){
//                var grid =overlay[j];
//                $("resultShape").innerHTML=$("resultShape").innerHTML+(j+1)+"个点:("+grid.lng+","+grid.lat+");<br/>";
//            }
//        }
        var str="[";
        for(var i = 0; i < overlays.length; i++){
            var overlay=overlays[i].getPath();
            for(var j = 0; j < overlay.length; j++){
                var grid =overlay[j];
                str+="["+grid.lng+","+grid.lat+"]";
                if (j!==overlay.length-1){
                    str+=","
                }
            }
        }
        str+="]";
        document.getElementById("location").value=str;
    }
    function Editing(state){
        for(var i = 0; i < overlays.length; i++){
            state=='enable'?overlays[i].enableEditing():overlays[i].disableEditing();
        }
    }

    function  showPolygon(btn){

        map.addOverlay(polygon);   //增加多边形
        // overlays.push(polygon); //是否把该图像加入到编辑和删除行列
        btn.setAttribute('disabled','false');
        showText();
    }

    function save() {
        var str=document.getElementById("location").value;
        if (str===''||str===null){
            alert("数据为空，请描绘区域后再生成数据");
            return;
        }
        var win=frameElement.api.opener;
        win.$("#area").val(str);
        frameElement.api.close();
    }

</script>
</body>
</html>
